//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "TriggeredSwapExercise.h"
using namespace Cephei::QL::Models::Marketmodels::Callability;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/Callability/MarketModelParametricExercise.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::CTriggeredSwapExercise (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ exerciseTimes, Cephei::Core::IVector<Double>^ strikes) : CMarketModelParametricExercise(CTriggeredSwapExercise::typeid)
{
    try
    {
#ifdef HANDLE
        _phTriggeredSwapExercise = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        exerciseTimes->Lock();
        INativeVector<Double>^ _NCIexerciseTimes = exerciseTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCexerciseTimes = safe_cast<CDoubleVector^>(_NCIexerciseTimes);
        std::vector<QuantLib::Time>& _exerciseTimes = static_cast<std::vector<QuantLib::Time>&> (_NCexerciseTimes->GetReference ());
        strikes->Lock();
        INativeVector<Double>^ _NCIstrikes = strikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCstrikes = safe_cast<CDoubleVector^>(_NCIstrikes);
        std::vector<QuantLib::Rate>& _strikes = static_cast<std::vector<QuantLib::Rate>&> (_NCstrikes->GetReference ());
        _ppTriggeredSwapExercise = new boost::shared_ptr<QuantLib::TriggeredSwapExercise> (new QuantLib::TriggeredSwapExercise ( _rateTimes,  _exerciseTimes,  _strikes ));
        SetMarketModelParametricExercise (boost::dynamic_pointer_cast<QuantLib::MarketModelParametricExercise> (*_ppTriggeredSwapExercise));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
        if (exerciseTimes != nullptr) exerciseTimes->Unlock();    //not optional
        if (strikes != nullptr) strikes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::CTriggeredSwapExercise (boost::shared_ptr<QuantLib::TriggeredSwapExercise>& childNative, Object^ owner) : CMarketModelParametricExercise(CTriggeredSwapExercise::typeid)
{
#ifdef HANDLE
	_phTriggeredSwapExercise = NULL;
#endif
	_ppTriggeredSwapExercise = &childNative;
    _ppMarketModelParametricExercise = new boost::shared_ptr<QuantLib::MarketModelParametricExercise> (boost::dynamic_pointer_cast<QuantLib::MarketModelParametricExercise> (*_ppTriggeredSwapExercise));
}
Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::CTriggeredSwapExercise (QuantLib::TriggeredSwapExercise& childNative, Object^ owner) : CMarketModelParametricExercise(CTriggeredSwapExercise::typeid)
{
#ifdef HANDLE
	_phTriggeredSwapExercise = NULL;
#endif
	_ppTriggeredSwapExercise = new boost::shared_ptr<QuantLib::TriggeredSwapExercise> (&childNative);
    _ppMarketModelParametricExercise = new boost::shared_ptr<QuantLib::MarketModelParametricExercise> (boost::dynamic_pointer_cast<QuantLib::MarketModelParametricExercise> (*_ppTriggeredSwapExercise));
    _TriggeredSwapExerciseOwner = owner;
    _MarketModelParametricExerciseOwner = owner;
}

Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::CTriggeredSwapExercise (CTriggeredSwapExercise^ copy) : CMarketModelParametricExercise(CTriggeredSwapExercise::typeid)
{
#ifdef HANDLE
	_phTriggeredSwapExercise = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTriggeredSwapExercise = new boost::shared_ptr<QuantLib::TriggeredSwapExercise> (copy->GetShared());
        _ppMarketModelParametricExercise = new boost::shared_ptr<QuantLib::MarketModelParametricExercise> (boost::dynamic_pointer_cast<QuantLib::MarketModelParametricExercise> (*_ppTriggeredSwapExercise));
    }
}
Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::CTriggeredSwapExercise (PLATFORM::Type^ t) : CMarketModelParametricExercise(CTriggeredSwapExercise::typeid)
{
#ifdef HANDLE
	_phTriggeredSwapExercise = NULL;
#endif
	if (!t->IsSubclassOf(CTriggeredSwapExercise::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::CTriggeredSwapExercise (QuantLib::Handle<QuantLib::TriggeredSwapExercise>& childNative, Object^ owner)  : CMarketModelParametricExercise(CTriggeredSwapExercise::typeid)
{
	_phTriggeredSwapExercise = &childNative;
	_ppTriggeredSwapExercise = &static_cast<boost::shared_ptr<QuantLib::TriggeredSwapExercise>>(childNative.currentLink());
    _ppMarketModelParametricExercise = new boost::shared_ptr<QuantLib::MarketModelParametricExercise> (boost::dynamic_pointer_cast<QuantLib::MarketModelParametricExercise> (*_ppTriggeredSwapExercise));
    _TriggeredSwapExerciseOwner = owner;
}
Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::CTriggeredSwapExercise (QuantLib::Handle<QuantLib::TriggeredSwapExercise> childNative)  : CMarketModelParametricExercise(CTriggeredSwapExercise::typeid)
{
	_phTriggeredSwapExercise = &childNative;
	_ppTriggeredSwapExercise = &static_cast<boost::shared_ptr<QuantLib::TriggeredSwapExercise>>(childNative.currentLink());
    _ppMarketModelParametricExercise = new boost::shared_ptr<QuantLib::MarketModelParametricExercise> (boost::dynamic_pointer_cast<QuantLib::MarketModelParametricExercise> (*_ppTriggeredSwapExercise));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::CTriggeredSwapExercise (QuantLib::TriggeredSwapExercise childNative)  : CMarketModelParametricExercise(CTriggeredSwapExercise::typeid)
{
#ifdef HANDLE
	_phTriggeredSwapExercise = NULL;
#endif
	_ppTriggeredSwapExercise = new boost::shared_ptr<QuantLib::TriggeredSwapExercise> (new QuantLib::TriggeredSwapExercise (childNative));
    _ppMarketModelParametricExercise = new boost::shared_ptr<QuantLib::MarketModelParametricExercise> (boost::dynamic_pointer_cast<QuantLib::MarketModelParametricExercise> (*_ppTriggeredSwapExercise));
}
#endif

Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::~CTriggeredSwapExercise ()
{
    if (_ppTriggeredSwapExercise != NULL)
    {
	    delete _ppTriggeredSwapExercise;
        _ppTriggeredSwapExercise = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::!CTriggeredSwapExercise ()
{
    if (_ppTriggeredSwapExercise != NULL)
    {
	    delete _ppTriggeredSwapExercise;
    }
}
QuantLib::TriggeredSwapExercise& Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::GetReference ()
{
    if (_ppTriggeredSwapExercise == NULL) throw REFNEW NativeNullException ();
	return **_ppTriggeredSwapExercise;
}
boost::shared_ptr<QuantLib::TriggeredSwapExercise>& Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::GetShared ()
{
    if (_ppTriggeredSwapExercise == NULL) throw REFNEW NativeNullException ();
	return *_ppTriggeredSwapExercise;
}
QuantLib::TriggeredSwapExercise* Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::GetPointer ()
{
    if (_ppTriggeredSwapExercise == NULL) throw REFNEW NativeNullException ();
	return &**_ppTriggeredSwapExercise;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TriggeredSwapExercise>& Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::GetHandle ()
{
	if (_phTriggeredSwapExercise == NULL)
	{
		_phTriggeredSwapExercise = new Handle<QuantLib::TriggeredSwapExercise> (*_ppTriggeredSwapExercise);
	}
	return *_phTriggeredSwapExercise;
}
#endif
bool Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::HasNative () 
{
	return (_ppTriggeredSwapExercise != NULL);
}

Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppTriggeredSwapExercise)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::Exercise (UInt64 exerciseNumber, Cephei::Core::IVector<Double>^ parameters, Cephei::Core::IVector<Double>^ variables)
{
    try
    {
        QuantLib::Size _exerciseNumber = (QuantLib::Size)ValueHelper::Convert (exerciseNumber); //a
        parameters->Lock();
        INativeVector<Double>^ _NCIparameters = parameters->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCparameters = safe_cast<CDoubleVector^>(_NCIparameters);
        std::vector<QuantLib::Real>& _parameters = static_cast<std::vector<QuantLib::Real>&> (_NCparameters->GetReference ());
        variables->Lock();
        INativeVector<Double>^ _NCIvariables = variables->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvariables = safe_cast<CDoubleVector^>(_NCIvariables);
        std::vector<QuantLib::Real>& _variables = static_cast<std::vector<QuantLib::Real>&> (_NCvariables->GetReference ());
    	bool _rv = (bool)(*_ppTriggeredSwapExercise)->exercise ( _exerciseNumber,  _parameters,  _variables );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (parameters != nullptr) parameters->Unlock(); //NO2  
        if (variables != nullptr) variables->Unlock(); //NO2  
    }
}
Cephei::QL::Models::Marketmodels::Callability::ITriggeredSwapExercise^ Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::Guess (UInt64 exerciseNumber, Cephei::Core::IVector<Double>^ parameters)
{
    try
    {
        QuantLib::Size _exerciseNumber = (QuantLib::Size)ValueHelper::Convert (exerciseNumber); //a
        parameters->Lock();
        INativeVector<Double>^ _NCIparameters = parameters->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCparameters = safe_cast<CDoubleVector^>(_NCIparameters);
        std::vector<QuantLib::Real>& _parameters = static_cast<std::vector<QuantLib::Real>&> (_NCparameters->GetReference ());
    	(*_ppTriggeredSwapExercise)->guess ( _exerciseNumber,  _parameters );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (parameters != nullptr) parameters->Unlock(); //NO2  
    }
}
Cephei::QL::Models::Marketmodels::Callability::ITriggeredSwapExercise^ Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::NextStep (Cephei::QL::Models::Marketmodels::ICurveState^ prm1)
{
    CCurveState^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CCurveState^> (prm1);
        _Cprm1->Lock();
        QuantLib::CurveState& _prm1 = static_cast<QuantLib::CurveState&> (_Cprm1->GetReference ()); 
    	(*_ppTriggeredSwapExercise)->nextStep ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::NumberOfExercises::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTriggeredSwapExercise)->numberOfExercises ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::NumberOfParameters::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppTriggeredSwapExercise)->numberOfParameters ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::NumberOfVariables::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppTriggeredSwapExercise)->numberOfVariables ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::ITriggeredSwapExercise^ Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppTriggeredSwapExercise)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::ITriggeredSwapExercise^ Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise::Values (Cephei::QL::Models::Marketmodels::ICurveState^ state, Cephei::Core::IVector<Double>^ results)
{
    CCurveState^ _Cstate;
    try
    {
        _Cstate = safe_cast<CCurveState^> (state);
        _Cstate->Lock();
        QuantLib::CurveState& _state = static_cast<QuantLib::CurveState&> (_Cstate->GetReference ()); 
        results->Lock();
        INativeVector<Double>^ _NCIresults = results->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCresults = safe_cast<CDoubleVector^>(_NCIresults);
        std::vector<QuantLib::Real>& _results = static_cast<std::vector<QuantLib::Real>&> (_NCresults->GetReference ());
    	(*_ppTriggeredSwapExercise)->values ( _state,  _results );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cstate != nullptr) _Cstate->Unlock();
        if (results != nullptr) results->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Callability::ITriggeredSwapExercise^ Cephei::QL::Models::Marketmodels::Callability::CTriggeredSwapExercise_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ exerciseTimes, Cephei::Core::IVector<Double>^ strikes)
{
    return REFNEW CTriggeredSwapExercise ( rateTimes,  exerciseTimes,  strikes);
}
